


let goodsId = 10959909
var request = require('request');
let categoryId = "105166"
// breadcrumbTrail(goodsId)
getPcGoodsDetailDynamic(goodsId,categoryId)


function skuDetail(body) {
    let body_json =  JSON.parse(body);
    let skuResultList = []
    skuDetailList = body_json.data.skuDetailList;
    for (let j = 0; j < skuDetailList.length; j++) {
        let skuResult = {}
        let  skuItem =  skuDetailList[j] 
        skuResult.goodsId = skuItem.skuInfo.goodsId
        skuResult.skuId = skuItem.skuInfo.skuId
        skuResult.name = getSkuName(skuItem.skuInfo.skuPropertyList)
        skuResultList.push(skuResult)
    }
    return skuResultList
}

function getSkuName(skuPropertyList) {
    let skuName = ""
    for (let k = 0; k < skuPropertyList.length; k++) {
        let  skuProperty =  skuPropertyList[k] ;
        if(k!=0){
            skuName += "--";
        }
        skuName += skuProperty.propertyValue ;
    }
    return skuName;
}

function getCategoryId(body) {
    let body_json =  JSON.parse(body);
    categoryList = body_json.retdata.categoryList;
    category = categoryList[categoryList.length-1]
    console.log(category);
    let categoryId = category.categoryId

    return categoryId;
}

function breadcrumbTrailCallback(error, response, body) {
    let categoryId = ""
    if (!error && response.statusCode == 200) {
        console.log("breadcrumbTrail结果：")
        console.log(body);
        categoryId = getCategoryId(body)  
        console.log("categoryId:"+categoryId)
        
        if(categoryId!=""){
            setTimeout(getPcGoodsDetailDynamic,1000,goodsId,categoryId)
            // getPcGoodsDetailDynamic(goodsId,categoryId)
        }
    }
}


function breadcrumbTrail(goodsId){
    var time1 = Date.parse( new Date() ).toString();
        time1 = time1.substr(0,13);
    var headers = {
        'Host': 'goods.kaola.com',
        'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
        'accept': 'application/json',
        'x-requested-with': 'XMLHttpRequest',
        'sec-ch-ua-mobile': '?0',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
        'content-type': 'application/json',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://goods.kaola.com/product/'+goodsId+'.html',
        'accept-language': 'zh-CN,zh;q=0.9,und;q=0.8',
};
    
    var req_url = 'https://goods.kaola.com/product/breadcrumbTrail/'+goodsId+'.json?t='+time1
    console.log(req_url);
    var options = {
        url: req_url,
        headers: headers
    };


    request(options, breadcrumbTrailCallback);
}

function getPcGoodsDetailDynamic(goodsId,categoryId){
var headers = {
    'Host': 'goods.kaola.com',
    'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
    'x-requested-with': 'XMLHttpRequest',
    'sec-ch-ua-mobile': '?0',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
    'content-type': 'application/x-www-form-urlencoded',
    'accept': '*/*',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'cors',
    'sec-fetch-dest': 'empty',
    'referer': 'https://goods.kaola.com/product/'+goodsId+'.html',
    'accept-language': 'zh-CN,zh;q=0.9,und;q=0.8',
};
var time1 = Date.parse( new Date() ).toString();
        time1 = time1.substr(0,13);
var req_url = 'https://goods.kaola.com/product/getPcGoodsDetailDynamic.json?provinceCode=350000&cityCode=350100&districtCode=350102&goodsId='+goodsId+'&categoryId='+categoryId+'&t='+time1
console.log(req_url);
var options = {
    url: req_url,
    headers: headers
};
request(options, getPcGoodsDetailDynamiccallback);
}

function getPcGoodsDetailDynamiccallback(error, response, body) {
    console.log("请求状态："+response.statusCode);
    if (!error && response.statusCode == 200) {
        console.log(body);
        let skuResultList = skuDetail(body)
        console.log(skuResultList);
    }
}